package com.smarttourism.controller;

import com.smarttourism.common.core.domain.R;
import com.smarttourism.domain.Checkin;
import com.smarttourism.service.CheckInService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/checkin")
public class CheckInController {
    @Autowired
    private CheckInService checkInService;

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('services:customer:list')")
    public R addCherkIn(@RequestBody Checkin checkin) throws ParseException {
        int i = checkInService.addCherkIn(checkin);
        if (i > 0) {
            return R.ok("新增成功");
        } else return R.fail("新增失败");
    }

    @GetMapping("list")
    public R findCheckIn() {
        List<Checkin> allCheckin = checkInService.findAllCheckin();
        if (allCheckin != null) {
            return R.ok(allCheckin);
        } else return R.fail("查询失败");
    }

    @PutMapping("checkout")
    public R checkOut(@Param("checkInId") Integer checkInId, @Param("roomNum") Integer roomNum) {
        int i = checkInService.checkOut(checkInId, roomNum);
        if (i > 0) {
            return R.ok("退房完成");
        } else return R.fail("退房失败");
    }

    @PutMapping("delete")
    public R deleteCheckIn(@Param("id") Integer id) {
        int i = checkInService.deleteCheckIn(id);
        if (i > 0) {
            return R.ok("删除成功");
        } else return R.fail("删除失败");
    }

}
